<?php

namespace app\logi\controller;

use cmf\controller\HomeBaseController;
use kuaidi\Kuaidi100;
use wxpay\Notify;
use weixiao\Jssdk;
use think\Db;

//接口控制器
class ApiController extends HomeBaseController {

    public function _initialize() {
        parent::_initialize();

    }

    /**
     * 获取省
     */
    public function getProvince() {
        $province = Db::name('region')->field('id,name')->where(array('level' => 1))->cache(true)->select();
        $res = array('status' => 1, 'msg' => '获取成功', 'result' => $province);
        exit(json_encode($res));
    }

    /**
     * 获取市或者区
     */
    public function getRegionByParentId() {
        $parent_id = $this->request->param('parent_id','');
        $res = array('status' => 0, 'msg' => '获取失败，参数错误', 'result' => '');
        if($parent_id){
            $region_list = Db::name('region')->field('id,name')->cache(true)->where(['parent_id'=>$parent_id])->select();
            $res = array('status' => 1, 'msg' => '获取成功', 'result' => $region_list);
        }
        exit(json_encode($res));
    }

    /**
     * 获取价格
     */
    public function getPrice() {
        $param = $this->request->param();
        $return = array('status' => 0, 'msg' => '获取失败，参数错误', 'result' => '');
        if($param){
            $list = [];
            $res = Db::name('logiPrice')->where(['status'=>1,'id'=>$param['style']])->find();
            if (!empty($res)) {
                $list['piece'] = $res['piece']*$param['weight'];
                $list['price'] = $res['price']*$param['weight'];
                $list['total'] = $list['piece']+$list['price'];
            }

            $return = array('status' => 1, 'msg' => '获取成功', 'result' => $list);
        }
        exit(json_encode($return));
    }

    /**
     * 获取编号对应的信息
     */
    public function getCode() {
        $param = $this->request->param();
        $return = array('status' => 0, 'msg' => '获取失败，参数错误', 'result' => '');
        if($param){
            $addr = Db::name('Address')->where(['fin_code'=>$param['code']])->field('contact,receive,zone')->find();
            if (!empty($addr)) {
                $list = [];
                $list['contact'] = has_plus($addr['zone']).' '.$addr['contact'];
                $list['receive'] = $addr['receive'];
                $list['user_id'] = substr($param['code'],0,-6); //用户id
                $list['user_addr'] = substr($param['code'],-1,1); //用户收货地址
                $list['stage'] = substr($param['code'],-6,2); //驿站
                $list['station'] = substr($param['code'],-4,3);  //小站
                $user_name = Db::name('user')->where(['id'=>$list['user_id']])->value('user_nickname');
                $list['user_name'] = $list['user_id'].' '.$user_name;
                $list['user_addr_arr'] = Db::name('address')->field('id,name')->where(['user_id'=>$list['user_id'],'code'=>$list['user_addr'],'type'=>3,'status'=>1])->find();
                $list['stage_arr'] = Db::name('address')->field('id,name,city_id')->where(['code'=>$list['stage'],'type'=>1])->find();
                $list['station_arr'] = Db::name('address')->field('id,name,city_id')->where(['code'=>$list['station'],'type'=>2])->find();
                $stage_city = cmf_get_region_name($list['stage_arr']['city_id']);
                $station_city = cmf_get_region_name($list['station_arr']['city_id']);
                $list['post_title'] = $stage_city.' - '.$station_city;

                $return = array('status' => 1, 'msg' => '获取成功', 'result' => $list);
            } else {
                $return = array('status' => 0, 'msg' => '获取失败，参数错误', 'result' => '');
            }
            
        }
        exit(json_encode($return));
    }

    /**
     * 获取快递信息
     */
    public function getExpress() {
        // com : "emsguoji"
        // condition : "F00"
        // data :
        //     0 : {time: "2018-08-05 12:38:50", ftime: "2018-08-05 12:38:50", context: "[延边朝鲜族自治州]投递并签收，签收人：本人收"}
        //     1 : {time: "2018-07-30 17:51:00", ftime: "2018-07-30 17:51:00", context: "[法国]离开处理中心,发往中国 北京"}
        //     2 : {time: "2018-07-30 17:35:00", ftime: "2018-07-30 17:35:00", context: "收寄"}
        // ischeck : "1"
        // message : "ok"
        // nu : "EY210424906FR"
        // state : "3"
        // status : "200"

        //{"result":false,"returnCode":"500","message":"查询无结果，请隔段时间再查"}

        //$kd = new Kuaidi100();
        //$res = $kd->track(['com'=>'emsguoji', 'num'=>'EY210424906FR']);
        //exit($res);
    }

    public function paySucceed() {
        $pay = new Notify();
        $pay->Handle();
        // $logi = session('logi_order');
        // if (!empty($logi)) {
        //     $param = [
        //         'id'=>$logi['id'], 
        //         'user_id'=>$logi['user_id'],
        //         'form_id'=>$logi['form_id'],
        //         'status_name'=>'待',
        //         'total'=>$logi['price'],
        //     ];
        //     cmf_push_msg($param);
        //     session('logi_order',null);  //清空
        // }
    }
}
